package com.sina.shop.controller.order;

import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.domain.order.Order;
import com.sina.shop.domain.order.Status;
import com.sina.shop.service.order.OrderService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-04-12-18:35
 */
@Controller
@RequestMapping("/shop-web/order")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/updateOrder", method = RequestMethod.POST)
    public ModelAndView updateOrder(Order order) {
        LOG.info("order:{}", order);

        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            orderService.updateOrder(order);
            result.put(CODE, OK);
            result.put(MSG, "更新订单成功");
        } catch (UpdateException e) {
            LOG.info("更新订单出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "/updateOrderStatus", method = RequestMethod.POST)
    public ModelAndView updateOrderStatus(@RequestParam("adminId") Long adminId, @RequestParam("status") Status status) {
        LOG.info("adminId:{}，status:{}", adminId, status);

        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            orderService.updateOrderStatus(adminId, status);
            result.put(CODE, OK);
            result.put(MSG, "更新订单状态成功");
        } catch (UpdateException e) {
            LOG.info("更新订单状态出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "/getOrderListByAdminId", method = RequestMethod.GET)
    public ModelAndView getOrderListByAdminId(@RequestParam("adminId") Long adminId, @RequestParam("status") Status status) {
        LOG.info("adminId:{}，status:{}", adminId, status);
        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> data = new HashMap<>(4);
        Map<String, Object> result = new HashMap<>(6);
        List<Order> orderList = orderService.getOrderListByAdminId(adminId, status);

        data.put("orderList", orderList);
        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "/getOrder", method = RequestMethod.GET)
    public ModelAndView getOrder(@RequestParam("id") Long id) {
        LOG.info("id:{}", id);
        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> data = new HashMap<>(4);
        Map<String, Object> result = new HashMap<>(6);
        Order order = orderService.getOrder(id);

        data.put("order", order);
        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "/saveOrder", method = RequestMethod.POST)
    public ModelAndView saveOrder(Order order) {
        LOG.info("order:{}", order);
        order.setCreateTime(LocalDateTime.now());

        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            orderService.saveOrder(order);
            result.put(CODE, OK);
            result.put(MSG, "新增订单成功");
        } catch (UpdateException e) {
            LOG.info("新增订单出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "/deleteOrder", method = RequestMethod.POST)
    public ModelAndView deleteOrder(@RequestParam("id") Long id) {
        LOG.info("id:{}", id);
        Map<String, Object> result = new HashMap<>(6);
        ModelAndView mv = new ModelAndView("commonView");

        try {
            orderService.deleteOrder(id);
            result.put(CODE, OK);
            result.put(MSG, "删除订单成功");
        } catch (UpdateException e) {
            LOG.info("删除订单出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }
}
